package com.euond.accv.mdm.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.euond.accv.mdm.client.dto.MdmStoreDTO;
import com.euond.accv.mdm.domain.MdmStoreGroup;
import com.euond.accv.mdm.dto.MdmStoreGroupDTO;
import com.euond.accv.mdm.service.MdmStoreGroupService;
import com.euond.common.api.controller.BaseReadApiController;
import com.euond.common.domain.search.SearchOperator;
import com.euond.common.domain.search.Searchable;
import com.euond.common.web.controller.annotation.FeignRestController;

import io.swagger.annotations.ApiOperation;

@FeignRestController
@RequestMapping("/feign_api/mdm_store_group")
public class MdmStoreGroupApiController extends BaseReadApiController<MdmStoreGroup, Integer>{
	
	@Autowired
	public MdmStoreGroupService service;
	
	@Override
	protected void init() {
		setBaseService(service);
		setMainVOClazz(MdmStoreGroupDTO.class);
	}
	
	@ApiOperation("加载群组树只加载第一级")
	@GetMapping("/findGrop.json")
	public List<MdmStoreGroupDTO> findStoreGrop(@RequestParam(value = "storeGroupNo", required = false)  
    String storeGroupNo){
		Searchable searchable =  Searchable.newSearchable().
				addSort(new Sort(Sort.Direction.DESC, "pStoreGroupNo"));
		 searchable.addSearchFilter("pStoreGroupNo", SearchOperator.eq, "1");
        List<MdmStoreGroupDTO> list = service.findMdmGrop(searchable); 
        return list;
	}
	
	@ApiOperation("获取当前节点下面的的数据")
	@GetMapping("/findTreeById.json")
	public List<MdmStoreGroupDTO> findStoreGropById(@RequestParam(value = "storeGroupNo", required = true)  
    String storeGroupNo){
		Searchable searchable =  Searchable.newSearchable().
				addSort(new Sort(Sort.Direction.DESC, "pStoreGroupNo"));
		 searchable.addSearchFilter("pStoreGroupNo", SearchOperator.eq, storeGroupNo);
        List<MdmStoreGroupDTO> list = service.findMdmGropById(searchable,0); 
        return list;
	}
	
	@ApiOperation("更加点击的树节点加载下面所有的叶子节点,只处理一级")
	@GetMapping("/findStoreGropIsLeafByPNo.json")
	public List<MdmStoreDTO> findStoreGropIsLeafByPNo(@RequestParam("pStoreGroupNo") String pStoreGroupNo,
			HttpServletRequest req){
		//需要ID转换成pStoreGroupNo
		Searchable searchable =  Searchable.newSearchable()
				.addSearchFilter("pStoreGroupNo", SearchOperator.eq, pStoreGroupNo);
		searchable.addSearchFilter("isLeaf", SearchOperator.eq, 1);
		searchable.addSort(new Sort(Sort.Direction.ASC, "orderNo"));
        List<MdmStoreDTO> page = service.findStoreGropIsLeafByPNo(searchable);
        return page;
	}
	
	
	@ApiOperation("更加点击的树节点加载下面所有的叶子节点,可以处理多级")
	@GetMapping("/findStoreGropIsLeafListByPNo.json")
	public List<MdmStoreDTO> findStoreGropIsLeafListByPNo(@RequestParam("pStoreGroupNo") String pStoreGroupNo,
			HttpServletRequest req){
		//需要ID转换成pStoreGroupNo
		Searchable searchable =  Searchable.newSearchable()
				.addSearchFilter("pStoreGroupNo", SearchOperator.eq, pStoreGroupNo);
		//searchable.addSearchFilter("isLeaf", SearchOperator.eq, 1);
		searchable.addSort(new Sort(Sort.Direction.ASC, "orderNo"));
        List<MdmStoreDTO> page = service.findStoreGropIsLeaf(searchable);//findStoreGropIsLeafByPNo(searchable);
        System.out.println("============="+page.size());
        return page;
	}
	
}